package com.winning.cqs.pregnantbabycare_cqs_v1.view;




import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.winning.cqs.pregnantbabycare_cqs_v1.R;
import com.winning.cqs.pregnantbabycare_cqs_v1.adapter.NumericWheelAdapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("SimpleDateFormat")
public class ActionSheet {

	private Dialog mDialog;
	private View mContentView;
	private LinearLayout mActionSheetContainer;
	private Context mContext;
	private WheelView year;
	private WheelView month;
	private WheelView day;
	private WheelView time;
	private WheelView min;
	private WheelView sec;
	private int mMaxyear;
	private int mYear=1996;
	private int mMonth=0;
	private int mDay=1;
	private String nowTime;
	private int n_year,n_month,n_day,n_sec,n_hour;
	private String mStime;
	 View view=null;	
    boolean isMonthSetted=false,isDaySetted=false,isSelect=false;
    
	public interface getTimeInterface {
		abstract void onGet(Object data);
	}
    

public ActionSheet(Context context,Boolean bSelect,int mMaxyear,String nowTime) {

		mContentView = LayoutInflater.from(context).inflate(R.layout.actionsheet_container, null);
		mActionSheetContainer = (LinearLayout) mContentView.findViewById(R.id.ly_action_sheet_container);

		mContext = context;
		isSelect = bSelect;
		mStime = nowTime;
		
		
		this.nowTime = nowTime;
		this.mMaxyear = mMaxyear;
		getDataPick();
	}
	
	private View getDataPick() {
		SimpleDateFormat sdfdate = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdftime = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		int curYear,curMonth,curDate,curHour,curMin,curSec;
		Calendar calendar = Calendar.getInstance();
		if(nowTime!=null&&nowTime!="")
		{
			if(isSelect)
			{
				try {
					Date date = sdftime.parse(nowTime);
					calendar.setTime(date);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else
			{
				try {
					Date date = sdfdate.parse(nowTime);
					calendar.setTime(date);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}	
		}	
		else
		{
			Date dNow = new Date();
			calendar.setTime(dNow);
			if(isSelect)
			{
				mStime = sdftime.format(dNow);
			}	
			else
			{
				mStime = sdfdate.format(dNow);
			}	
		}	
		 curYear = calendar.get(Calendar.YEAR);
		 curMonth = calendar.get(Calendar.MONTH) + 1;//ͨ��Calendar���������Ҫ+1
		 curDate = calendar.get(Calendar.DATE);
		 curHour = calendar.get(Calendar.HOUR_OF_DAY);
		 curMin = calendar.get(Calendar.MINUTE);
		 curSec  =calendar.get(Calendar.SECOND);
		
//		int curYear = mYear;
//		int curMonth =mMonth+1;
//		int curDate = mDay;
		
		//view = inflater.inflate(R.layout.wheel_date_picker, null);
		
		year = (WheelView)mContentView. findViewById(R.id.year);
		NumericWheelAdapter numericWheelAdapter1=new NumericWheelAdapter(mContext,1880, mMaxyear); 
		numericWheelAdapter1.setLabel("年");
		year.setViewAdapter(numericWheelAdapter1);
		year.setCyclic(true);//�Ƿ��ѭ������
		year.addScrollingListener(scrollListener);
		
		month = (WheelView) mContentView.findViewById(R.id.month);
		NumericWheelAdapter numericWheelAdapter2=new NumericWheelAdapter(mContext,1, 12, "%02d"); 
		numericWheelAdapter2.setLabel("月");
		month.setViewAdapter(numericWheelAdapter2);
		month.setCyclic(true);
		month.addScrollingListener(scrollListener);
		
		day = (WheelView) mContentView.findViewById(R.id.day);
		initDay(curYear,curMonth);
		day.setCyclic(true);
		day.addScrollingListener(scrollListener);		
//		time= (WheelView) view.findViewById(R.id.time);
//		String[] times = {"����","����"} ;
//		ArrayWheelAdapter<String> arrayWheelAdapter=new ArrayWheelAdapter<String>(MainActivity.this,times );
//		time.setViewAdapter(arrayWheelAdapter);
//		time.setCyclic(false);
//		time.addScrollingListener(scrollListener);
		
	    	
		
		min = (WheelView) mContentView.findViewById(R.id.min);
		NumericWheelAdapter numericWheelAdapter3=new NumericWheelAdapter(mContext,1, 23, "%02d"); 
		numericWheelAdapter3.setLabel("时");
		min.setViewAdapter(numericWheelAdapter3);
		min.setCyclic(true);
		
		
		sec = (WheelView) mContentView.findViewById(R.id.sec);
		NumericWheelAdapter numericWheelAdapter4=new NumericWheelAdapter(mContext,1, 59, "%02d"); 
		numericWheelAdapter4.setLabel("分");
		sec.setViewAdapter(numericWheelAdapter4);
		sec.setCyclic(true);
		
		if (!isSelect)
	    {
			min.setVisibility(View.GONE);
			sec.setVisibility(View.GONE);
	    }
		else
		{
			min.setVisibleItems(7);
			sec.setVisibleItems(7);
			min.addScrollingListener(scrollListener);
			sec.addScrollingListener(scrollListener);
		}	
	    
	    
		
		
		year.setVisibleItems(7);//������ʾ����
		month.setVisibleItems(7);
		day.setVisibleItems(7);
//		time.setVisibleItems(7);

		
		year.setCurrentItem(curYear - 1880);
		month.setCurrentItem(curMonth - 1);
		day.setCurrentItem(curDate - 1);
		min.setCurrentItem(curHour-1);
		sec.setCurrentItem(curMin-1);
		
		return view;
	}
	
	private void initDay(int arg1, int arg2) {
		NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(mContext,1, getDay(arg1, arg2), "%02d");
		numericWheelAdapter.setLabel("日");
		day.setViewAdapter(numericWheelAdapter);
	}
	
	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
		@Override
		public void onScrollingStarted(WheelView wheel) {

		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			 n_year = year.getCurrentItem() + 1880;//��
			 n_month = month.getCurrentItem() + 1;//��
			 n_day = day.getCurrentItem()+1;
			 n_hour = min.getCurrentItem()+1;
			 n_sec = sec.getCurrentItem()+1;
		     mStime = null;
              
		     if (isSelect)
		     { 	 
			 mStime =new StringBuilder().append((year.getCurrentItem()+1880)).append("-").append((month.getCurrentItem() + 1) < 10 ? "0" + (month.getCurrentItem() + 1) : (month.getCurrentItem() + 1)).append("-").append(((day.getCurrentItem()+1) < 10) ? "0" + (day.getCurrentItem()+1) : (day.getCurrentItem()+1)).append(" ").append(((min.getCurrentItem()+1) < 10) ? "0" + (min.getCurrentItem()+1) : (min.getCurrentItem()+1)).append(":").append(((sec.getCurrentItem()+1) < 10) ? "0" + (sec.getCurrentItem()+1) : (sec.getCurrentItem()+1)).toString();
		     }
		     else
		     {
		     mStime=new StringBuilder().append((year.getCurrentItem()+1880)).append("-").append((month.getCurrentItem() + 1) < 10 ? "0" + (month.getCurrentItem() + 1) : (month.getCurrentItem() + 1)).append("-").append(((day.getCurrentItem()+1) < 10) ? "0" + (day.getCurrentItem()+1) : (day.getCurrentItem()+1)).toString();
		     } 
			 
			 
			 //			 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//			 try {
//				Date birthdayDate = sdf.parse(mStime);
//				if (birthdayDate.getTime()>1) {
//				
//				}
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			tv1.setText("����             "+calculateDatePoor(birthday)+"��");
//			tv2.setText("����             "+getAstro(month.getCurrentItem() + 1,day.getCurrentItem()+1));
		}
	};
	
	/**
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private int getDay(int year, int month) {
		int day = 30;
		boolean flag = false;
		switch (year % 4) {
		case 0:
			flag = true;
			break;
		default:
			flag = false;
			break;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = flag ? 29 : 28;
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}


	public void addAction(String actionStr, final getTimeInterface handler) {
	//	ViewGroup view = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.actionsheet_button, null);

		TextView actionView = (TextView) mContentView.findViewById(R.id.action_sure);
		//actionView.setText(actionStr);
		actionView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				handler.onGet(mStime);
				mDialog.dismiss();
			}
		});
//		mActionSheetContainer.addView(view);
	}

	public void show() {

	//	ViewGroup view = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.actionsheet_cancel, null);
		TextView actionView = (TextView) mContentView.findViewById(R.id.action_cancel);
		actionView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mDialog.dismiss();
			}
		});

	//	mActionSheetContainer.addView(view);

		DisplayMetrics metrics = new DisplayMetrics();
		Rect rect = new Rect();

		Activity activity = ((Activity) mContext);
		WindowManager windowManager = activity.getWindowManager();
		windowManager.getDefaultDisplay().getMetrics(metrics);
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

		mDialog = new Dialog(mContext, R.style.ActionSheetStyle);
		mDialog.setContentView(mContentView);
		mDialog.getWindow().setGravity(Gravity.BOTTOM);
		mDialog.getWindow().getAttributes().width = metrics.widthPixels;
		mDialog.getWindow().getAttributes().height = metrics.heightPixels - rect.top;
		mDialog.show();
	}
}